//
//  FriendManagerViewController.m
//  TuiTe
//
//  Created by 张 霄鹤 on 10-6-3.
//  Copyright 2010 xhzhang. All rights reserved.
//

#import "FriendManagerViewController.h"
#import "TwitterClient.h"


@implementation FriendManagerViewController

@synthesize segControl;
@synthesize friends;
@synthesize followers;

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/


- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
	
	self.navigationItem.title = NSLocalizedString(@"FRIENDS",@"");
	
	// UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithCustomView:self.segControl];
	self.navigationItem.titleView = self.segControl;
	// [btn release];
	
	NSMutableArray *ma = [[NSMutableArray alloc] init];
	self.friends = ma;
	[ma release];
	
	ma = [[NSMutableArray alloc] init];
	self.followers = ma;
	[ma release];
	
	segIndex = 0;
}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	[self.followers removeAllObjects];
	[self.friends removeAllObjects];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	self.segControl = nil;
	self.followers = nil;
	self.friends = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSInteger ret = 0;
	if(section == 0){
		switch (segIndex) {
			case 0:
				ret = [self.friends count];
				break;
			case 1:
				ret = [self.followers count];
				break;
			default:
				break;
		}
		if(ret > 0){
			ret = ret - 2;
		}
	}
    return ret;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Set up the cell...
	NSArray *ids = nil;
	if(segIndex == 0){
		// Friends
		ids = self.friends;
	}else if(segIndex == 1){
		// Followers
		ids = self.followers;
	}
	
	NSInteger row = indexPath.row;
	NSString *ss = @"";
	if (ids) {
		ss = [ids objectAtIndex:(row + 2)];
	}
	cell.textLabel.text = ss;
	
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
}



// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}




// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
		NSMutableArray *arr=nil;
		if(segIndex == 0){
			arr = self.friends;
		}else if(segIndex == 1){
			arr = self.followers;
		}
		if (arr) {
			[arr removeObjectAtIndex:(indexPath.row + 2)];
		}
		// TODO 进行用户管理
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}



/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

- (IBAction)segmentAction:(id)sender{
	// The segmented control was clicked, handle it here 
	// TODO 处理点击事件用于显示上一条，下一条
	UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
	//NSLog(@"Segment clicked: %d", segmentedControl.selectedSegmentIndex);
	NSInteger si = segmentedControl.selectedSegmentIndex;
	segIndex = si;
	NSString *ns = si == 0 ? @"0" : @"1";
	[NSThread detachNewThreadSelector:@selector(getIDs:) toTarget:self withObject:ns];
}

- (void)getIDs:(NSString *)sType{
	TwitterClient *tc;
	NSArray *ids;
	NSInteger si = 0;
	if([sType isEqualToString:@"1"]){
		si = 1;
	}
	switch (si) {
		case 0:
			// TODO 显示或者获取用户的Friends（用户Follow的） Twitter API: friends/ids.xml
			tc = [TwitterClient getInstance];
			ids = [tc doFriendShipIDS:0 withCursor:@"-1"];
			if(ids){
				[self.friends setArray:ids];
			}
			break;
		case 1:
			// TODO 显示或者获取Follow用户的人. Twitter API: followers/ids.xml
			tc = [TwitterClient getInstance];
			ids = [tc doFriendShipIDS:1 withCursor:@"-1"];
			if(ids){
				[self.followers setArray:ids];
			}
			break;
		default:
			break;
	}
	[self.tableView reloadData];
}

- (void)dealloc {
	[segControl release];
	[followers release];
	[friends release];
    [super dealloc];
}


@end

